package com.splic.product.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.github.pagehelper.Page;
import com.splic.common.core.DeviceContext;
import com.splic.common.exception.BusinessException;
import com.splic.common.utils.DateUtils;
import com.splic.product.domain.*;
import com.splic.product.dto.GoodsDto;
import com.splic.product.dto.UserFeedbackDto;
import com.splic.product.service.*;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.splic.product.mapper.UserFeedbackMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户反馈记录Service业务层处理
 * 
 * @author singe
 * @date 2022-01-04
 */
@Service
public class UserFeedbackServiceImpl implements UserFeedbackService
{
    @Autowired
    private UserFeedbackMapper userFeedbackMapper;
    @Autowired
    private EmailSendCodeService emailSendCodeServiceImpl;
    @Autowired
    private DeviceService deviceServiceImpl;
    @Autowired
    private UserService userServiceImpl;
    @Autowired
    private ProductService productServiceImpl;

    /**
     * 查询用户反馈记录
     * 
     * @param id 用户反馈记录主键
     * @return 用户反馈记录
     */
    @Override
    public UserFeedback selectUserFeedbackById(Long id)
    {
        return userFeedbackMapper.selectUserFeedbackById(id);
    }

    /**
     * 查询用户反馈记录列表
     * 
     * @param userFeedback 用户反馈记录
     * @return 用户反馈记录
     */
    @Override
    public List<UserFeedbackDto> selectPageList(UserFeedback userFeedback)
    {
        Page<UserFeedbackDto> userFeedbackDtoPage = new Page<>();

        Page<UserFeedback> userFeedbackPage = userFeedbackMapper.selectUserFeedbackList(userFeedback);
        if(CollectionUtils.isNotEmpty(userFeedbackPage)){
            Set<Long> prodcutIdSet = userFeedbackPage.stream().map(UserFeedback::getProductId).collect(Collectors.toSet());
            Map<Long,String> productMap = null;
            if(CollectionUtils.isNotEmpty(prodcutIdSet)){
                List<Product> productList = productServiceImpl.getByIds(prodcutIdSet);
                if(CollectionUtils.isNotEmpty(productList)){
                    productMap = productList.stream().collect(Collectors.toMap(Product::getId,Product::getName));
                }
            }

            UserFeedbackDto dto = null;
            for(UserFeedback g :userFeedbackPage){
                dto = new UserFeedbackDto();
                BeanUtils.copyProperties(g,dto);
                if(productMap != null){
                    dto.setProductName(productMap.get(g.getProductId()));
                }
                userFeedbackDtoPage.add(dto);
            }
        }
        userFeedbackDtoPage.setTotal(userFeedbackPage.getTotal());
        return userFeedbackDtoPage;
    }

    /**
     * 查询用户反馈记录列表
     *
     * @param userFeedback 用户反馈记录
     * @return 用户反馈记录
     */
    @Override
    public List<UserFeedback> selectUserFeedbackList(UserFeedback userFeedback)
    {
        return userFeedbackMapper.selectUserFeedbackList(userFeedback);
    }

    /**
     * 新增用户反馈记录
     * 
     * @param userFeedback 用户反馈记录
     * @return 结果
     */
    @Override
    public int insert(UserFeedback userFeedback)
    {
        userFeedback.setCreateTime(DateUtils.getNowDate());
        return userFeedbackMapper.insert(userFeedback);
    }

    /**
     * 修改用户反馈记录
     * 
     * @param userFeedback 用户反馈记录
     * @return 结果
     */
    @Override
    public int update(UserFeedback userFeedback)
    {
        userFeedback.setUpdateTime(DateUtils.getNowDate());
        return userFeedbackMapper.update(userFeedback);
    }


    /**
     * 删除用户反馈记录信息
     * 
     * @param id 用户反馈记录主键
     * @return 结果
     */
    @Override
    public int deleteById(Long id)
    {
        return userFeedbackMapper.deleteById(id);
    }
    /**
     * 修改用户反馈记录
     *
     * @param userFeedback 用户反馈记录信息
     * @return 结果
     */
    @Override
    @Transactional
    public int saveOrUpdate(UserFeedback userFeedback)
    {
        if(userFeedback.getId() != null){
            userFeedback.setUpdateTime(new Date());
            return userFeedbackMapper.update(userFeedback);
        }else{
            userFeedback.setCreateTime(new Date());
            return userFeedbackMapper.insert(userFeedback);
        }
    }

    @Override
    public void saveFeedback(UserFeedback userFeedback) {
        User user = userServiceImpl.getByUserIdentify(DeviceContext.getUserIdentify());
        if (user != null) {
            userFeedback.setUserId(user.getId());
        }

        Device device = deviceServiceImpl.getByDeviceIdentify(DeviceContext.getDeviceIdentify());
        if (device != null){
            userFeedback.setDeviceId(device.getId());
        }

        Product product = productServiceImpl.getByPackageKey(DeviceContext.getProductPackageKey());
        if (product == null){
            throw new BusinessException("应用查询失败。");
        }
        userFeedback.setProductId(product.getId());
        userFeedback.setProductVersion(DeviceContext.getVersion());
        int count = this.insert(userFeedback);

        //发送邮件
        emailSendCodeServiceImpl.sendEmailFeedback(userFeedback,product.getName());

        if(count <= 0){
            throw new BusinessException("用户反馈记录保存失败。");
        }
    }
}
